layui.use(['jquery', 'laypage', 'layer'], function () {
    let $ = layui.jquery,
        laypage = layui.laypage,
        layer = layui.layer;
    if (localStorage.getItem('Authorization')) {
        let text = '<a href="#" style="color: white">' + localStorage.getItem('name') + '</a>'
        $('.navbar-right li').eq(1).html(text)
        $('#photo').attr('src', localStorage.getItem('photo'))
    }
    let count = 0
    let countAjax = $.ajax({
        type: "GET",
        url: '/api/article/count',
        dataType: 'json',
        data: {
            title: decodeURI(getUrlParam("title"), "utf-8"),
            classify: decodeURI(getUrlParam("classify"), "utf-8")
        },
        success: function (data) {
            if (data.status === 200) {
                console.log(data.data)
                count = data.data.count
            } else {
                layer.msg(data.msg)
            }
        },
        err: function () {
            layui.use('layer', function () {
                layer.msg("服务器请求失败")
            });
        }
    });
    $.when(countAjax).done(function () {
        console.log(count)
        laypage.render({
            elem: 'paging'
            , count: count
            , limit: 3
            , jump: function (obj) {
                $.ajax({
                    type: "GET",
                    url: '/api/article/listArticle',
                    dataType: 'json',
                    data: {
                        page: obj.curr,
                        limit: 3,
                        title: decodeURI(getUrlParam("title"), "utf-8"),
                        classify: decodeURI(getUrlParam("classify"), "utf-8")
                    },
                    success: function (data) {
                        if (data.status === 200) {
                            data = data.data
                            let html = ""
                            for (let i = 0; i < data.length; ++i) {
                                let text =
                                    ' <div class="post-item">\n' +
                                    '                    <h1>\n' +
                                    '                        <a href="/article?id=' + data[i].id + '">\n' +
                                    '                            ' + data[i].title +
                                    '                        </a>\n' +
                                    '                    </h1>\n' +
                                    '                    <div class="create">\n' +
                                    '                        <span>Created</span>\n' +
                                    '                        <time>\n' +
                                    '                            ' + data[i].date + '\n' +
                                    '                        </time>\n' +
                                    '                    </div>\n' +
                                    '                   ' + data[i].content +
                                    '                    <div class="readmore">\n' +
                                    '                        <a href="/article?id=' + data[i].id + '">\n' +
                                    '                            Read More\n' +
                                    '                        </a>\n' +
                                    '                    </div>\n' +
                                    '                </div>'
                                html += text
                            }
                            $('#recent-posts').html(html)
                        } else {
                            layer.msg(data.msg)
                        }
                    },
                    err: function () {
                        layui.use('layer', function () {
                            layer.msg("服务器请求失败")
                        });
                    }
                });
            }
        });
    })
});

function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return r[2];
    return null;
}